package com.example.demo2database;


import com.example.demo2database.m.DormRepository;
import com.example.demo2database.u.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.example.demo2database.tools.MD5.getMD5;

@Controller //控制器
@RequestMapping(path="/")
public class LoginController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private DormRepository dormRepository;
    private Cookie cookie =new Cookie("Username","");


    @GetMapping(path ="/login")
    public @ResponseBody
    String userLogin (@RequestParam String username , @RequestParam String password, HttpServletResponse response,HttpServletRequest request){
        //密码进行MD5加密
        password =getMD5(password);

        if(password.equals(userRepository.findpassword(username))){
            HttpSession session = request.getSession();
            request.getSession().setAttribute("userInfo", username + " - " + password);
            cookie.setValue("logined");
            //cookie.setValue(session.getId());
            cookie.setMaxAge(300);
            response.addCookie(cookie);
            return "登录成功";
        }
        return "登录失败，用户名或密码错误";
    }

    @GetMapping(path="/list")
    public @ResponseBody void helloCookie(HttpServletResponse resp, HttpServletRequest req) throws IOException {
//        Cookie[] cs = req.getCookies();
//        System.out.println(cs);
//        System.out.println("--------------");
//        for (Cookie c : cs) {
//            resp.getWriter().write(String.valueOf(c));
//        }
        resp.setContentType("text/html;charset=UTF-8");
        String value = cookie.getValue();  //可以成功得到对应的cookies ,只要在同级路径下就可以

        HttpSession session = req.getSession();
        System.out.println("sessionId为：" + session.getId());
        Object userInfo = session.getAttribute("userInfo");
        if (userInfo == null) {
            System.out.println("获取宿舍列表失败，您还没有登录，请重新登录");
            resp.getWriter().write("获取宿舍列表失败，您还没有登录，请重新登录");
        } else {
            System.out.println("已经登录过啦，用户信息为：" + session.getAttribute("userInfo"));
            resp.getWriter().write("即将为您返回宿舍列表");
            resp.getWriter().write("当前的空房间个数为："+dormRepository.countBNums());
            resp.getWriter().write("当前的空房间列表[楼栋号，房间号]为："+dormRepository.printBNums());

            System.out.println(dormRepository.findAll());
            System.out.println("----------------- dormRepository.printBNums()-----------------  ");
            System.out.println(dormRepository.printBNums());
            System.out.println(dormRepository.countBNums());
        }
    }
}
